package com.hsr.web.controller;


import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.hsr.web.config.ServerConfig;
import com.hsr.web.dto.FileDTO;
import com.hsr.web.dto.FileQuery;
import com.hsr.web.dto.ImageDTO;
import com.hsr.web.dto.ImageQuery;
import com.hsr.web.entity.TFile;
import com.hsr.web.entity.TImage;
import com.hsr.web.service.ITImageService;
import com.hsr.web.utils.FileUtil;
import com.hsr.web.utils.ResultVOUtil;
import com.hsr.web.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tb
 * @since 2022-04-02
 */
@RestController
@Api(tags = "图片相关接口")
@RequestMapping("/image")
public class TImageController {
    @Autowired
    ITImageService imageService;
    @Autowired
    ServerConfig serverConfig;

    @GetMapping("/list")
    @ApiOperation("查询列表")
    public ResultVO list(ImageQuery query) {
        return ResultVOUtil.success(imageService.getList(query));
    }

    @SneakyThrows
    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public ResultVO upload(@RequestParam("file") MultipartFile multipartFile,@RequestParam("type") String type){
        String uuid = UUID.randomUUID().toString().replace("-","");
        String imageName = uuid+multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        String filePath = FileUtil.fileUpload(multipartFile, "/apps/img/",imageName);
        TImage image=new TImage();
        image.setId(uuid);
        image.setCreateTime(LocalDateTime.now());
        image.setImageUrl(filePath);
        image.setImageType(type);
        imageService.save(image);
        return ResultVOUtil.success(serverConfig.getUrl()+filePath);
    }


}